package Reminder.domain;

import com.alibaba.fastjson.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class User {
    private int userId;
    private String phone;
    private String userName;
    private Date birthday;
    private boolean gender;
    private String job;
    private int age;
    public User(){}

    @Override
    public String toString() {
        return toJSON().toString();
    }

    public User(int userId, String userName, boolean gender, int age, Date birthday, String phone, String job) {
        this.userId = userId;
        this.phone = phone;
        this.userName = userName;
        this.birthday = birthday;
        this.gender = gender;
        this.job = job;
        this.age = age;
    }
    public User(JSONObject userInfo) throws ParseException {
        if (userInfo.containsKey("userId"))
            this.userId = Integer.parseInt(userInfo.get("userId").toString());
        if (userInfo.containsKey("phone"))
            this.phone = userInfo.get("phone").toString();
        if (userInfo.containsKey("userName"))
            this.userName = userInfo.get("userName").toString();
        if (userInfo.containsKey("birthday"))
            this.birthday = new SimpleDateFormat("yyyy-MM-dd").parse(userInfo.get("birthday").toString());
        if (userInfo.containsKey("gender"))
            this.gender = Integer.parseInt(userInfo.get("gender").toString()) == 1;
        if (userInfo.containsKey("job"))
            this.job = userInfo.get("job").toString();
        if (userInfo.containsKey("age"))
            this.age = Integer.parseInt(userInfo.get("age").toString());
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public boolean getGender() {
        return gender;
    }

    public void setGender(boolean gender) {
        this.gender = gender;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public JSONObject toJSON(){
        JSONObject res = new JSONObject();
        res.put("userId",getUserId());
        res.put("phone",getPhone());
        res.put("userName",getUserName());
        res.put("birthday",getBirthday());
        res.put("gender",getGender());
        res.put("job",getJob());
        res.put("age",getAge());
        return res;
    }

}
